#******************************************************************************
#******************************************************************************

# import libraries

from math import isclose, pi

import datetime as dt

from src.pysoleng.models import tilted_plane_irradiance_isotropic

from src.pysoleng.timeplace import Location, FlatSurface

# *****************************************************************************
# *****************************************************************************

class TestIrradianceModels:

    def test_isotropic_model(self):
        
        # *********************************************************************
        # *********************************************************************
        
        # Example 2.15.1, page 
        # Using the isotropic diffuse model, estimate the beam, diffuse, and 
        # ground-reﬂected components of solar radiation and the total radiation
        # on a surface sloped 60◦ toward the south at a latitude of 40◦ N for 
        # the hour 9 to 10 AM on February 20. Here I = 1.04 MJ/m2 and ρ_g = 0.6
        
        # >>> from datetime import datetime
        # >>> datetime.fromisoformat('2011-11-04T00:05:23+04:00')  
        
        site_ground_reflectance = 0.6
        site_horizontal_total = 1.04 # MJ/m2
        site_horizontal_diffuse = site_horizontal_total*0.766 # kt=0.445
        
        surface = FlatSurface(
            azimuth=0*pi/180, 
            slope=60*pi/180
            )
        
        site = Location(
            coordinates=(40,0), 
            crs_string='epsg:4326')
        
        local_time = dt.datetime.fromisoformat('2011-02-20 09:30:00+00:00')
        
        # final results (expected)
        
        efr_itt = 1.17 # MJ/m2 # original: 1.170
        
        efr_itt_tol = 0.01 # MJ/m2
        
        efr_itb = 0.417 # MJ/m2
        
        efr_itb_tol = 0.01 # MJ/m2
        
        efr_itsd = 0.596 # MJ/m2
        
        efr_itsd_tol = 0.01 # MJ/m2
        
        efr_ita = 0.156 # MJ/m2
        
        efr_ita_tol = 0.01 # MJ/m2
        
        # *********************************************************************
        # *********************************************************************
        
        (I_tilted_total, 
         I_tilted_beam, 
         I_tilted_sky_diffuse, 
         I_tilted_albedo) = tilted_plane_irradiance_isotropic(
             site, 
             #surface,
             surface.slope,
             surface.azimuth,
             local_time, 
             site_ground_reflectance, 
             site_horizontal_total, 
             site_horizontal_diffuse)
             
        # *********************************************************************
        # *********************************************************************
        
        # total
        assert isclose(I_tilted_total, efr_itt, abs_tol=efr_itt_tol)
        
        # beam
        assert isclose(I_tilted_beam, efr_itb, abs_tol=efr_itb_tol)
        
        # sky diffuse
        assert isclose(I_tilted_sky_diffuse, efr_itsd, abs_tol=efr_itsd_tol)
        
        # albedo
        assert isclose(I_tilted_albedo, efr_ita, abs_tol=efr_ita_tol)
                
        # *********************************************************************
        # *********************************************************************

# *****************************************************************************
# *****************************************************************************